package com.leetcode.search.dfs;

/**
 * @author Dennis Li
 * @date 2020/7/26 9:33
 */
public class MaxAreaOfIsland_695 {

    private final int[][] directions = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};

    public int maxAreaOfIsland(int[][] grid) {
        int high = grid.length, width = grid[0].length;
        int max = 0;
        for (int i = 0; i < high; i++) {
            for (int j = 0; j < width; j++) {
                if (grid[i][j] == 1)
                    max = Math.max(dfs(grid, i, j), max);
            }
        }
        return max;
    }

    public int dfs(int[][] grid, int a, int b) {
        if (grid[a][b] == 0) return 0;
        // 不需要额外建立一个boolean，直接赋值为0即可
        grid[a][b] = 0;
        int sum = 1;
        for (int[] d : directions) {
            int x = a + d[0], y = b + d[1];
            // 划定网格的边界条件，并且注意是不能出现过
            if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length)
                sum += dfs(grid, x, y);
        }
        return sum;
    }

}
